﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using System.Xml;

using LuaInterface;

namespace Codeology.Auctioneer
{

    public class AuctioneerFaction
    {

        private AuctioneerRealm realm;
        private AuctioneerFactionType faction_type;
        private AuctioneerScanStats scan_stats;
        private List<AuctioneerItem> auction_items;

        internal AuctioneerFaction(AuctioneerRealm factionRealm, AuctioneerFactionType factionType, LuaTable factionTable)
        {
            // Set realm
            realm = factionRealm;

            // Set faction type
            faction_type = factionType;

            // Get scan statistics table
            LuaTable scan_stats_table = (LuaTable)factionTable["scanstats"];

            // Create scan statistics
            scan_stats = new AuctioneerScanStats(scan_stats_table);

            // Create somewhere to put auction items
            auction_items = new List<AuctioneerItem>();

            // Get ropes table
            LuaTable ropes_table = (LuaTable)factionTable["ropes"];

            // Create somewhere to copy ropes
            StringBuilder builder = new StringBuilder();

            // Process ropes
            foreach(DictionaryEntry ropes_entry in ropes_table) {
                using (Lua lua = new Lua()) {
                    // Get objects from ropes
                    object[] objects = lua.DoString(ropes_entry.Value.ToString());

                    // Get rope table
                    LuaTable rope_table = (LuaTable)objects[0];

                    // Process auction items
                    foreach(DictionaryEntry rope_entry  in rope_table) {
                        // Get auction item
                        LuaTable item_table = (LuaTable)rope_entry.Value;

                        // Create auction item
                        AuctioneerItem item = new AuctioneerItem(item_table);

                        // Add auction item to items
                        auction_items.Add(item);
                    }
                }
            }
        }

        #region Methods

        public override string ToString()
        {
            return faction_type.ToString();
        }

        #endregion

        #region Properties

        public AuctioneerFactionType FactionType
        {
            get {
                return faction_type;
            }
        }

        public AuctioneerScanStats ScanStats
        {
            get {
                return scan_stats;
            }
        }

        public AuctioneerItem[] AuctionItems
        {
            get {
                return auction_items.ToArray();
            }
        }

        #endregion

    }

}
